﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1_5
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //是否已登入
                if (Session["login"] != null)
                {
                    var sl = (SortedList<string, string>)Session["login"];

                    userName.Text = sl["useName"];

                    //初始化添加和编辑前所在的分页数
                    if (Session["PageIndex"] != null)
                    {
                        GridView1.PageIndex = Convert.ToInt32(Session["PageIndex"]);
                    }

                    //选中GridView1更新和新增的行
                    if (Request.QueryString["cancel"] != null && Session["PageIndex"] != null)
                    {
                        var isCancel = Convert.ToBoolean(Request.QueryString["cancel"]);
                        if (!isCancel)
                        {
                            GridView1.SelectedIndex = (int)Session["index"];
                        }

                    }

                    Display();
                }
                else
                {
                    Response.Write("<script>alert('你还未登入请登入!!!!');location.assign('Login.aspx');</script>");
                }

            }
            else
            {
                GridView1.SelectedIndex = -1;
            }
        }

        /// <summary>
        /// GridView1数据的初始化
        /// </summary>
        private void Display()
        {
            string sql = "select * from Articles";
            GridView1.DataSource = DbHelper.GetData(sql);
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 实现点击分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Session["PageIndex"] = e.NewPageIndex;

            Display();
        }



        /// <summary>
        /// 全选处理事件，注意需要添加 AutoPostBack="true"属性，
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            var isAllChecked = ((CheckBox)GridView1.HeaderRow.Cells[0].Controls[1]).Checked;

            for (int i = 0; i < GridView1.PageSize; i++)
            {

                if (isAllChecked)
                {
                    ((CheckBox)(GridView1.Rows[i].Cells[0].Controls[1])).Checked = true;
                }
                else
                {
                    ((CheckBox)(GridView1.Rows[i].Cells[0].Controls[1])).Checked = false;
                }
            }

        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Session["index"] = e.NewEditIndex;

            var id = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
            Response.Redirect("Iudata.aspx?id=" + id);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addBtn_Click(object sender, EventArgs e)
        {



            var count = DbHelper.GetData("select * from Articles").Rows.Count;

            Session["index"] = count % GridView1.PageSize;
            Session["PageIndex"] = Math.Ceiling(count / (GridView1.PageSize * 1.0));

            Response.Redirect("Iudata.aspx");

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void delBtn_Click(object sender, EventArgs e)
        {
            string str = "";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                var isDelete = ((CheckBox)(GridView1.Rows[i].Cells[0].Controls[1])).Checked;
                if (isDelete)
                {
                    str += GridView1.DataKeys[i].Value + ",";
                }

            }

            if (str != "")
            {
                str = str.Substring(0, str.Length - 1);
                string sql = string.Format("delete  Articles where id in ({0})", str);
                var res = DbHelper.UpdateData(sql);


                if (res < 0)
                {
                    Response.Write("<script>alert('删除失败')</script>");
                }

                Display();

            }
            else
            {
                Response.Write("<script>alert('请勾选要删除的行')</script>");
            }
        }
    }
}